{{#operations}}//
// {{classname}}.swift
{{>licenseInfo}}

import Foundation{{#usePromiseKit}}
import PromiseKit{{/usePromiseKit}}{{#useRxSwift}}
import RxSwift{{/useRxSwift}}{{#useCombine}}
#if canImport(Combine)
import Combine
#endif{{/useCombine}}{{#useVapor}}
import Vapor{{/useVapor}}
#if canImport(AnyCodable)
import AnyCodable
#endif{{#swiftUseApiNamespace}}

extension {{projectName}}API {
{{/swiftUseApiNamespace}}

{{#description}}
/** {{{.}}} */{{/description}}
{{#objcCompatible}}@objc {{/objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class {{classname}}{{#objcCompatible}} : NSObject{{/objcCompatible}} {

    weak var client: {{projectName}}Client?

    public init(client: {{projectName}}Client) {
        self.client = client
    }
{{#operation}}
    {{#allParams}}
    {{#isEnum}}

    /**
     * enum for parameter {{paramName}}
     */
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum {{enumName}}_{{operationId}}: {{^isContainer}}{{{dataType}}}{{/isContainer}}{{#isContainer}}String{{/isContainer}}, CaseIterable{{#useVapor}}, Content{{/useVapor}} {
        {{^enumUnknownDefaultCase}}
        {{#allowableValues}}
        {{#enumVars}}
        case {{name}} = {{{value}}}
        {{/enumVars}}
        {{/allowableValues}}
        {{/enumUnknownDefaultCase}}
        {{#enumUnknownDefaultCase}}
        {{#allowableValues}}
        {{#enumVars}}
        {{^-last}}
        case {{name}} = {{{value}}}
        {{/-last}}
        {{/enumVars}}
        {{/allowableValues}}
        {{/enumUnknownDefaultCase}}
    }
    {{/isEnum}}
    {{/allParams}}
{{^useVapor}}
{{^usePromiseKit}}
{{^useRxSwift}}
{{^useResult}}
{{^useCombine}}
{{^useAsyncAwait}}

    /**
     {{#summary}}
     {{{.}}}
     {{/summary}}{{#allParams}}
     - parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}
     - parameter apiResponseQueue: The queue on which api response is dispatched.
     - parameter completion: completion handler to receive the data and the error objects
     */
    {{#isDeprecated}}
    @available(*, deprecated, message: "This operation is deprecated.")
    {{/isDeprecated}}
    @discardableResult
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func {{operationId}}Sync({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}[{{enumName}}_{{operationId}}]{{/isContainer}}{{^isContainer}}{{enumName}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}apiResponseQueue: DispatchQueue = {{projectName}}API.apiResponseQueue, completion: @escaping ((_ data: {{{returnType}}}{{^returnType}}Void{{/returnType}}?, _ error: Error?) -> Void)) -> RequestTask {
        return {{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}).execute(apiResponseQueue) { result in
            switch result {
            {{#returnType}}
            case let .success(response):
                completion(response.body, nil)
            {{/returnType}}
            {{^returnType}}
            case .success:
                completion((), nil)
            {{/returnType}}
            case let .failure(error):
                completion(nil, error)
            }
        }
    }

    /**
     {{#summary}}
     {{{.}}}
     {{/summary}}{{#allParams}}
     - parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}
     - parameter apiResponseQueue: The queue on which api response is dispatched.
     - parameter completion: completion handler to receive the data and the error objects
     */
    {{#isDeprecated}}
    @available(*, deprecated, message: "This operation is deprecated.")
    {{/isDeprecated}}
    @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
    private class func {{operationId}}AsyncMappedParams({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}[{{enumName}}_{{operationId}}]{{/isContainer}}{{^isContainer}}{{enumName}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{^-last}}, {{/-last}}{{/allParams}}) async throws -> {{{returnType}}}{{^returnType}}Void{{/returnType}} {
        return try await withCheckedThrowingContinuation { continuation in
            {{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}).execute { result in
                switch result {
                {{#returnType}}
                case let .success(response):
                    continuation.resume(returning: response.body)
                {{/returnType}}
                {{^returnType}}
                case .success:
                    continuation.resume(returning: ())
                {{/returnType}}
                case let .failure(error):
                    continuation.resume(throwing: error)
                }
            }
        }
    }

{{> api_operation_comments}}
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func {{operationId}}(
{{> api_operation_signature}}
{{> api_operation_implementation}}

{{> api_operation_comments}}
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func {{operationId}}(
{{> api_operation_signature}}
{{> api_operation_implementation}}

{{/useAsyncAwait}}
{{/useCombine}}
{{/useResult}}
{{/useRxSwift}}
{{/usePromiseKit}}
{{/useVapor}}
{{#usePromiseKit}}
{{> api_operation_promise_kit}}
{{/usePromiseKit}}
{{#useRxSwift}}
{{> api_operation_rx_swift}}
{{/useRxSwift}}
{{#useCombine}}
{{> api_operation_combine}}
{{/useCombine}}
{{#useAsyncAwait}}
{{> api_operation_async_await}}
{{/useAsyncAwait}}
{{#useResult}}
{{> api_operation_result}}
{{/useResult}}
{{#useVapor}}
{{> api_operation_vapor}}
{{/useVapor}}
{{^useVapor}}

{{> api_request_builder_comments}}
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func {{operationId}}WithRequestBuilder(
{{> api_request_builder_signature}}
        let basePath = {{projectName}}API.basePath;
{{> api_request_builder_setup_parameters}}
        do {
            {{#authMethods}}
            try Authentication.setAuthenticationParameters(headers: &localVariableHeaderParameters, url: &localVariableUrlComponents, in: "{{apiKeyIn}}", name: "{{keyParamName}}", value: {{projectName}}API.{{nameInPascalCase}}, prefix: {{#vendorExtensions.x-konfig-prefix}}"{{.}}"{{/vendorExtensions.x-konfig-prefix}}{{^vendorExtensions.x-konfig-prefix}}nil{{/vendorExtensions.x-konfig-prefix}})
            {{/authMethods}}
{{> api_request_builder_init}}
        } catch {
            print("Error: \(error)")
        }
        fatalError("Error: Unable to send request to {{httpMethod}} {{{path}}}")
    }

{{> api_request_builder_comments}}
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func {{operationId}}WithRequestBuilder(
{{> api_request_builder_signature}}
        let basePath = self.client!.basePath;
{{> api_request_builder_setup_parameters}}
        do {
            {{#authMethods}}
            try Authentication.setAuthenticationParameters(headers: &localVariableHeaderParameters, url: &localVariableUrlComponents, in: "{{apiKeyIn}}", name: "{{keyParamName}}", value: self.client!.{{nameInPascalCase}}, prefix: {{#vendorExtensions.x-konfig-prefix}}"{{.}}"{{/vendorExtensions.x-konfig-prefix}}{{^vendorExtensions.x-konfig-prefix}}nil{{/vendorExtensions.x-konfig-prefix}})
            {{/authMethods}}
{{> api_request_builder_init}}
        } catch {
            print("Error: \(error)")
        }
        fatalError("Error: Unable to send request to {{httpMethod}} {{{path}}}")
    }

{{/useVapor}}
{{/operation}}
}
{{#swiftUseApiNamespace}}
}
{{/swiftUseApiNamespace}}
{{/operations}}
